import {
  addFunction,
  getFunctionList,
  onoffFunction,
  updateFunction,
} from '../../services/channel/api';
import { message,} from 'antd';

export default {
  namespace: 'functionSlot',

  state: {
    loading: true,
    funcListData: {
      list: [],
      pagination:{}
    },
  },
  effects: {
    *fetchGetFunctionList({payload,callback}, { call, put }) {
      const response = yield call(getFunctionList, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            funcListData: {
              list: response.data.records,
              pagination:{
                current: response.data.current,
                pageSize:response.data.size,
                total: response.data.total,
              }
            }
          },
        });
        if (callback) callback();
      }else {
        message.error(response.message)
      }
    },

    *fetchAddFunction({payload,callback}, { call, put }) {
      const response = yield call(addFunction, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            addFuncData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.error(response.message)
      }

    },

    *fetchUpdateFunction({payload,callback}, { call, put }) {
      const response = yield call(updateFunction, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            updateFuncData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.error(response.message)
      }
    },

    *fetchOnOffFunction({payload,callback}, { call, put }) {
      const response = yield call(onoffFunction, payload);
      if (response && response.code == 200) {
        yield put({
          type: 'save',
          payload: {
            onOffData: response
          },
        });
        if (callback && response) callback(response);
      }else {
        message.error(response.message)
      }
    },

  },

  reducers: {
    save(state, { payload }) {
      return {
        ...state,
        ...payload,
      };
    },
  },

};
